package com.soul.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soul.common.exception.BaseException;
import com.soul.sys.bean.dto.dict.DictTypeAddDto;
import com.soul.sys.bean.dto.dict.DictTypeSearchDto;
import com.soul.sys.bean.dto.dict.DictTypeUpdateDto;
import com.soul.sys.bean.po.DictPo;
import com.soul.sys.bean.po.DictTypePo;
import com.soul.sys.bean.vo.DictTypeVo;
import com.soul.sys.mapper.DictTypeMapper;
import com.soul.sys.service.DictService;
import com.soul.sys.service.DictTypeService;
import com.soul.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author sws
 * @since 2023-09-27 10:08:12
 */
@Service
public class DictTypeServiceImpl extends BaseService<DictTypeMapper, DictTypePo> implements DictTypeService {

    @Autowired
    private DictService dictService;
    /**
     * 分页查询数据字典类型
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public IPage<DictTypeVo> queryDictTypeList(Integer currentPage, Integer size, DictTypeSearchDto searchDto){
        // 分页
        IPage<DictTypePo> iPage = new Page<>(currentPage,size);
        // 查询数据字典类型列表
        // 查询条件
        LambdaQueryWrapper<DictTypePo> queryWrapper = Wrappers.lambdaQuery(DictTypePo.class);
        if (ObjectUtil.isNotEmpty(searchDto)){
            queryWrapper.like(ObjectUtil.isNotEmpty(searchDto.getTypeCode()),DictTypePo::getTypeCode,searchDto.getTypeCode())
                    .like(ObjectUtil.isNotEmpty(searchDto.getTypeName()),DictTypePo::getTypeName,searchDto.getTypeName())
                    .orderByAsc(DictTypePo::getSort)
            ;

        }
        IPage<DictTypePo> typePage = this.page(iPage,queryWrapper);
//        List<DictTypeVo> dictTypeVos = Optional.ofNullable(typePage.getRecords()).orElse(new ArrayList<>()).stream()
//                .map(po-> BeanUtil.copyProperties(po, DictTypeVo.class)).collect(Collectors.toList());
        return typePage.convert(this::toDataVo);
    }

    @Override
    public DictTypeVo getDictTypeInfo(Long id){
        DictTypePo typePo = this.getById(id);
        return this.toDataVo(typePo);
    }

    @Override
    public void addDictType(DictTypeAddDto addDto){
        // 校验代码重复
        this.verifyCode(addDto.getTypeCode());
        DictTypePo dictTypePo = BeanUtil.copyProperties(addDto, DictTypePo.class);
        // 排序号
        Long maxSort = this.getMaxSort();
        dictTypePo.setSort(maxSort);
        this.save(dictTypePo);
    }

    @Override
    public void updateDictType(DictTypeUpdateDto updateDto){
        // 判断是否存在
        DictTypePo typePo = this.getById(updateDto.getId());
        if (ObjectUtil.isEmpty(typePo)){
            throw new BaseException("数据已不存在");
        }
        // 校验代码是否重复
        this.verifyCode(updateDto.getTypeCode(),updateDto.getId());
        BeanUtil.copyProperties(updateDto,typePo);
        this.updateById(typePo);
    }

    /**
     * 删除字典
     * @param id
     */
    @Override
    public void deleteDictType(Long id){
        DictTypePo typePo = this.getById(id);
        if (ObjectUtil.isEmpty(typePo)){
            throw new BaseException("数据不存在");
        }
        String typeCode = typePo.getTypeCode();
        // 查询字典中是否存在字典数据
        List<DictPo> dictPos = this.dictService.selectByTypeCode(typeCode);
        if (ObjectUtil.isNotEmpty(dictPos)){
            throw new BaseException("字典中还存在字典数据，无法删除！");
        }
        this.removeById(id);
    }
    private void verifyCode(String code,Long id){
        List<DictTypePo> typePos = this.getLambadaQueryWrapper().eq(DictTypePo::getTypeCode, code).list();
        if (ObjectUtil.isNotEmpty(typePos)){
            if (ObjectUtil.isEmpty(id)){
                throw new BaseException("代码不能重复");
            }
            List<DictTypePo> collect = typePos.stream().filter(p -> !Objects.equals(p.getId(), id)).collect(Collectors.toList());
            if (ObjectUtil.isNotEmpty(collect)){
                throw new BaseException("代码不能重复");
            }
        }
    }
    private void verifyCode(String code){
        this.verifyCode(code,null);
    }
    private DictTypeVo toDataVo(DictTypePo po){
        return BeanUtil.copyProperties(po, DictTypeVo.class);
    }
}
